package com.mdxq.mall.user.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.mdxq.mall.response.R;
import com.mdxq.mall.user.Dto.UserLoginDTO;
import com.mdxq.mall.user.service.AdminRoleRelationService;
import com.mdxq.mall.user.service.RoleService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.http.HttpStatus;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.mdxq.mall.user.entity.Admin;
import com.mdxq.mall.user.service.AdminService;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.mdxq.mall.user.entity.table.AdminTableDef.ADMIN;

/**
 * 后台用户表 控制层。
 *
 * @author zxr
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/api/v1/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("login")
    public R login(@RequestBody UserLoginDTO dto, HttpServletRequest request) {
        Admin admin = adminService.login(dto.getUsername(), dto.getPassword(), request);
        if (admin != null) {
            Map<String, Object> payload = new HashMap<>() {
                {
                    put("id", admin.getId());
                    put("username", admin.getUsername());
                    put("nikeName", admin.getNickName());
                    put("iat", System.currentTimeMillis());//令牌签发时间
                    put("exp", 1 * 60 * 60 * 1000L);//令牌的有效时长
                }
            };
            String token = JWTUtil.createToken(payload, "md-mall-0112".getBytes());//创建令牌
            // 向redis缓存中存入用户登录生成的token
            redisTemplate.opsForValue().set(token,null);
            redisTemplate.expire(token,10, TimeUnit.HOURS);
            return R.ok(token);//向客户端返回令牌
        }
        // HttpStatus.UNAUTHORIZED 表示 401，未授权
        return R.error();
    }
    @PostMapping("logout")
    public R logout(@RequestHeader("authorization") String token){
        redisTemplate.delete(token);
        //之后加上Redis,需要在登出系统时，让Redis中token过期
        return R.ok(true);
    }

    /**
     * 添加后台用户表。
     *
     * @param admin 后台用户表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("register")
    public R save(@RequestBody Admin admin) {
        admin.setCreateTime(LocalDateTime.now());
       if (adminService.save(admin)){
           return R.ok(admin);
       }
       return R.error();
    }

    /**
     * 根据主键删除后台用户表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("delete/{id}")
    public R remove(@PathVariable Long id) {
        return R.ok(adminService.removeById(id));
    }

    /**
     * 根据主键更新后台用户表。
     *
     * @param admin 后台用户表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update/{id}")
    public R update(@PathVariable Long id,@RequestBody Admin admin ) {

        if (adminService.updateById(admin)){
            return R.ok(admin);
        }else {
            return R.error();
        }
    }

    /**
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("updateStatus/{id}")
    public R updateStatus(@PathVariable Long id,@RequestParam int status){
        Admin admin = adminService.getById(id);
        admin.setStatus(status);
        adminService.updateById(admin);
        return R.ok(admin);

    }

    /**
     * 查询所有后台用户表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public R list(@RequestParam Admin admin) {

        return R.ok(adminService.list());
    }

    /**
     * 根据后台用户表主键获取详细信息。
     *
     * @return 后台用户表详情
     */
    @GetMapping("getInfo")
    public R getInfo(@RequestHeader("authorization") String token) {
        JWT jwt = JWTUtil.parseToken(token);
        int adminId = Integer.valueOf(jwt.getPayload("id").toString());
        Admin admin = adminService.getById(adminId);
        return R.ok(admin);
    }

    /**
     * 分页查询后台用户表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public R page(Page<Admin> page,String keyword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select().from(ADMIN).where(ADMIN.NICK_NAME.like(keyword)).or(ADMIN.USERNAME.like(keyword));
        return R.ok(adminService.page(page,queryWrapper));
    }

    @Autowired
    private AdminRoleRelationService relationService;
    @Autowired
    private RoleService roleService;
    /**
     *
     * @param id
     * @return
     */
    @GetMapping("role/{id}")
    public R getRoleByAdmin(@PathVariable Long id){
       QueryWrapper queryWrapper = new QueryWrapper();
       queryWrapper.eq("admin_id",id);
       queryWrapper.select("role_id");
       List<Long> roleIds =relationService.listAs(queryWrapper,Long.class);
       if(CollUtil.isNotEmpty(roleService.listByIds(roleIds))){
           return R.ok(roleService.listByIds(roleIds));
       }else {
           return R.ok(Collections.emptyList());
       }
    }
    @PostMapping("role/update")
    public R allocRole(@RequestParam Long adminId,@RequestParam String roleIds){
        return R.ok(adminService.allocRole(adminId,roleIds));
    }
}
